package com.easylinkin.linkappapi.knowledgebase.controller;


import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.knowledgebase.entity.KnowledgeType;
import com.easylinkin.linkappapi.knowledgebase.service.KnowledgeTypeService;
import com.easylinkin.linkappapi.knowledgebase.vo.KnowledgeTypeVO;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 知识类别表 前端控制器
 * </p>
 *
 * @author xy
 * @since 2021-12-02
 */
@RestController
@Api(value = "知识类别控制器", tags = {"知识类别控制器"})
@RequestMapping("/knowledgebase/knowledgeType")
public class KnowledgeTypeController {

  @Autowired
  private KnowledgeTypeService service;

  @PostMapping("getList")
  @ApiOperation("知识类别列表")
  public RestMessage getList(@RequestBody KnowledgeType knowledgeType) {
    Assert.notNull(knowledgeType, "customQueryParams 不能为空");
    List<KnowledgeTypeVO> record = service.getList(knowledgeType);
    return RestBuilders.successBuilder().data(record).build();
  }

  /**
   * 新增知识类别
   */
  @ApiOperation("新增知识类别")
  @PostMapping("add")
  @CommonOperateLogAnnotate(module = LogModule.KNOWLEDGE_TYPE, desc = "新增知识类别")
  public RestMessage addKnowledgeType(@RequestBody KnowledgeType knowledgeType) {
    checkParams(knowledgeType);
    service.addKnowledgeType(knowledgeType);
    return RestBuilders.successBuilder(knowledgeType).build();
  }

  /**
   * 更新知识类别
   */
  @ApiOperation("更新知识类别")
  @PutMapping("update")
  @CommonOperateLogAnnotate(module = LogModule.KNOWLEDGE_TYPE, desc = "修改知识类别")
  public RestMessage updateKnowledgeType(@RequestBody KnowledgeType knowledgeType) {
    checkParams(knowledgeType);
    Assert.notNull(knowledgeType.getId(), "id不能为空");
    service.updateKnowledgeType(knowledgeType);
    return RestBuilders.successBuilder().build();
  }

  /**
   * 根据id删除知识类别
   */
  @ApiOperation("根据id删除删除知识类别")
  @PostMapping("deleteKnowledgeTypeById")
  @CommonOperateLogAnnotate(module = LogModule.KNOWLEDGE_TYPE, desc = "删除知识类别")
  public RestMessage deleteKnowledgeTypeById(@RequestBody KnowledgeType knowledgeType) {
    Assert.notNull(knowledgeType.getId(), "ID不能为空");
    service.deleteKnowledgeTypeById(knowledgeType.getId());
    return RestBuilders.successBuilder().build();
  }

  /**
   * 校验数据
   */
  private void checkParams(KnowledgeType knowledgeType) {
    Assert.notNull(knowledgeType, "请求参数不能为空");
    Assert.notNull(knowledgeType.getName(), "name不能为空");
  }
}
